<template>
  <div id="app" class="container " v-cloak>
    <div class="card mt-2" v-for="index in textList" :key="index.id">
      <div class="card-header">
        <span class="float-left text-left text-info text-nowarp text-truncate"> {{index.articleTime}}</span>
        <span class="text-dark d-inline-block  text-center text-truncate">{{index.articleTitle}}</span>
        <span class="float-right ">
          <span class="text-info ">阅读数({{index.articleRead}})</span>
          <span class="text-info ">点赞量({{index.articleLike}})</span>
          <span class="text-info ">评论量({{index.articleCommentCounts}})</span>
        </span>
      </div>
      <div class="card-body text-body text-truncate " id="articleContent">
        <p v-html="index.articleContent"></p>
      </div>
      <div class="card-footer text-center">
        <router-link :to="{name:'edit',params:{articleid:index.articleId}}"><button class="btn btn-primary">编辑</button></router-link></router-link>
        <router-link :to="{name:'content',params:{articleid:index.articleId}}"><button class="btn btn-primary">查看</button></router-link></router-link>
        <button class="btn btn-primary" @click="deleteArticleOne(index.articleId)">删除</button>
      </div>
    </div>
    <Page v-if="pagetotal!=0" :total="pagetotal" @pagechange="pagechange"></Page>
  </div>
</template>

<script>
  import Page from '../public/Page.vue'
  export default {
    name: "ArticleMange",
    data() {
      return {
        urlfix: "http://127.0.0.1:9000",
        textList: [],
        pagetotal: 0
      }
    },
    methods: {
      deleteArticleOne(articleId) {
        let url = this.urlfix + "/personal/deleteArticleOne"
        let that = this
        let temdata = {
          userEmail: window.sessionStorage.getItem("userEmail"),
          userId: window.sessionStorage.getItem("userId"),
          articleId: articleId
        }
        $.post(url, temdata, function(data) {
          if(data.flag==true){
            location.reload()
          }
        })
      },
      pagechange(pagedata){
        let url = this.urlfix + "/personal/selectArticle"
        let that = this
        let data = {
          userId: window.sessionStorage.getItem("userId"),
          userEmail:window.sessionStorage.getItem("userEmail"),
          page:pagedata
        }
        $.post(url, data, function(data) {
          if (data.code === 20000) {
            that.textList=[]
            $.each(data.data, function(index, item) {
              console.log(that.textList)
              that.textList.push(item)
            })
          }
        })
      }

    },
    components: {
      Page
    },
    beforeMount() {
      let url = this.urlfix + "/personal/selectArticle"
      let that = this
      let data = {
        userId: window.sessionStorage.getItem("userId"),
        userEmail:window.sessionStorage.getItem("userEmail"),
        page:1
      }
      //去到总页数，每页4条记录
      $.post(that.urlfix+"/personal/selectArticleTotal",{ userId: window.sessionStorage.getItem("userId"),
        userEmail:window.sessionStorage.getItem("userEmail")},(data)=>{
          that.pagetotal=Math.ceil((parseInt(data.data))/4)
      })
      $.post(url, data, function(data) {
        if (data.code === 20000) {
          $.each(data.data, function(index, item) {
            that.textList.push(item)
          })
        }
      })
    }
  }
</script>

<style>
  #articleContent{
    height: 3.125rem;
    width: 100%;
    overflow: hidden;
  }
</style>
